package com.geline.mybatisplus.interceptor;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.sql.SQLException;
import java.util.Collection;

/**
 * Entity注解拦截器
 *
 * @author: mx
 * @date: 2024-09-06 14:21
 */
public abstract class EntityInterceptor<A extends Annotation> implements InnerInterceptor {

    protected final Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(this.getClass(), EntityInterceptor.class);
    protected final Class<A> clazz = this.currentAnnotaionClass();

    protected Class<A> currentAnnotaionClass() {
        return (Class<A>) this.typeArguments[0];
    }

    @Override
    public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        SqlCommandType sct = ms.getSqlCommandType();
        if (sct == SqlCommandType.INSERT) {
            this.handleEntity(sct, parameter);
        }else if (sct == SqlCommandType.UPDATE) {
            Object object = parameter;
            if(parameter instanceof MapperMethod.ParamMap){
                MapperMethod.ParamMap map = (MapperMethod.ParamMap) parameter;
                if(map.containsKey("et")){
                    object = map.get("et");
                }
            }
            this.handleEntity(sct, object);
        }else if (sct == SqlCommandType.DELETE) {
            if(ms.getId().contains("deleteById")){
                //com.geline.cloud.system.mapper.SysNoticeMapper
                String mapper = ms.getId().replace(".deleteById", "");
                Class<BaseMapper> mapperClass = ClassUtil.loadClass(mapper);
                BaseMapper baseMapper = SpringUtil.getBean(mapperClass);
                Object entity = baseMapper.selectById((Serializable) parameter);
                this.handleEntity(sct, entity);
            } else if(ms.getId().contains("deleteBatchIds")){
                String mapper = ms.getId().replace(".deleteBatchIds", "");
                Class<BaseMapper> mapperClass = ClassUtil.loadClass(mapper);
                BaseMapper baseMapper = SpringUtil.getBean(mapperClass);

                Object object = parameter;
                if(parameter instanceof MapperMethod.ParamMap){
                    MapperMethod.ParamMap map = (MapperMethod.ParamMap) parameter;
                    if(map.containsKey("et")){
                        object = map.get("et");
                    }else if(map.containsKey("param1")){
                        object = map.get("param1");
                    }
                }
                Collection list = (Collection)object;
                for(Object id : list){
                    Object entity = baseMapper.selectById((Serializable) id);
                    this.handleEntity(sct, entity);
                }
            }
        }
    }

    public void handleEntity(SqlCommandType sct, Object entity){
        A[] annArray = entity.getClass().getAnnotationsByType(clazz);
        if(annArray != null && annArray.length > 0) {
            for (A annotation : annArray) {
                if (sct == SqlCommandType.INSERT) {
                    this.handleInsert(annotation, entity);
                }else if (sct == SqlCommandType.UPDATE) {
                    this.handleUpdate(annotation, entity);
                }else if (sct == SqlCommandType.DELETE) {
                    this.handleDelete(annotation, entity);
                }
            }
        }
    }

    public abstract void handleInsert(A annotation, Object entity);

    public abstract void handleUpdate(A annotation, Object entity);

    public abstract void handleDelete(A annotation, Object entity);
}
